package com.yixin.ystartlibrary.widget;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.yixin.ystartlibrary.R;


/**
 * Created by: WDY
 * Date: 2017/3/16 11:33
 * Content: 提示信息对话框
 */

public class HintDialog extends AlertDialog {

    private TextView mTitleView;
    private TextView mHintView;
    private Button mCancelButton;
    private Button mConfirmButton;

    private String mTitleText;
    private String mHintText;
    private String mCancelText;
    private String mConfirmText;
    private int mHintTextColorRes = -1;
    private View.OnClickListener mCancelListener;
    private View.OnClickListener mConfirmListener;

    protected HintDialog(Context context) {
        super(context);
    }

    protected HintDialog(Context context, int theme) {
        super(context, theme);
    }

    protected HintDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_hint_dialog);
        setCancelable(true);
        setCanceledOnTouchOutside(true);
        initViews();
    }

    private void initViews() {
        mTitleView = (TextView) findViewById(R.id.hint_title_view);
        mHintView = (TextView) findViewById(R.id.hint_hint_text_view);
        mCancelButton = (Button) findViewById(R.id.hint_cancel_button);
        mConfirmButton = (Button) findViewById(R.id.hint_confirm_button);

        if (!TextUtils.isEmpty(mTitleText)) {
            mTitleView.setVisibility(View.VISIBLE);
            mTitleView.setText(mTitleText);
        }

        if (!TextUtils.isEmpty(mHintText)) {
            mHintView.setVisibility(View.VISIBLE);
            mHintView.setText(mHintText);
        }

        if (!TextUtils.isEmpty(mConfirmText)) {
            mConfirmButton.setVisibility(View.VISIBLE);
            mConfirmButton.setText(mConfirmText);
        }

        if (!TextUtils.isEmpty(mCancelText)) {
            mCancelButton.setVisibility(View.VISIBLE);
            mCancelButton.setText(mCancelText);
        }

        if (mHintTextColorRes != -1) {
            mHintView.setTextColor(getContext().getResources().getColor(mHintTextColorRes));
        }

        if (mCancelListener != null) {
            mCancelButton.setVisibility(View.VISIBLE);
            mCancelButton.setOnClickListener(mCancelListener);
        } else {
            mCancelButton.setOnClickListener(onlyDismissListener);
        }

        if (mConfirmListener != null) {
            mConfirmButton.setOnClickListener(mConfirmListener);
            mConfirmButton.setVisibility(View.VISIBLE);
        } else {
            mConfirmButton.setOnClickListener(onlyDismissListener);
        }

    }

    private View.OnClickListener onlyDismissListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dismiss();
        }
    };

    public static HintDialog create(Context context, CharSequence message,
                                    boolean cancelable, OnCancelListener cancelListener) {
        HintDialog dialog = new HintDialog(context);
        dialog.mHintText = message.toString();
        dialog.setCancelable(cancelable);
        dialog.setOnCancelListener(cancelListener);
        return dialog;
    }

    public static HintDialog show(Context context, CharSequence message,
                                  boolean cancelable, OnCancelListener cancelListener) {
        HintDialog dialog = create(context, message, cancelable, cancelListener);
        dialog.show();
        return dialog;
    }

    public HintDialog setTitleText(String title) {
        mTitleText = title;
        return this;
    }

    public HintDialog setHintText(String hint) {
        mHintText = hint;
        return this;
    }

    public HintDialog setHintTextColor(int colorRes) {
        mHintTextColorRes = colorRes;
        return this;
    }

    public HintDialog setConfirmText(String confirmText) {
        mConfirmText = confirmText;
        return this;
    }

    public HintDialog setCancelText(String cancelText) {
        mCancelText = cancelText;
        return this;
    }

    public HintDialog setConfirmListener(View.OnClickListener listener) {
        mConfirmListener = listener;
        return this;
    }

    public HintDialog setCancelListener(View.OnClickListener listener) {
        mCancelListener = listener;
        return this;
    }


}
